package com.mercenary.wein.screenControl.deviceControll;

import com.mercenary.wein.screenControl.ScreenPrint.ScreenCaptureThread;
import com.mercenary.wein.screenControl.bean.DeviceBean;
import com.mercenary.wein.screenControl.deviceControll.AndroidDevice;
import com.mercenary.wein.screenControl.deviceControll.OperateAndroid;
import com.mercenary.wein.screenControl.service.DeviceService;
import com.mercenary.wein.screenControl.service.GroupService;
import com.mercenary.wein.screenControl.ui.JPanelScreen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

/**
 * Created by tuxiao on 2017/3/2.
 */
public class DeviceControlDecorator extends JScrollPane {
    public static final int jpsWidth = 270;
    public static final int jpsHight = 480;
    private OperateAndroid oa;
    private JPanelScreen jPanelScreen;
    private static DeviceService deviceService = new DeviceService();
    final static GroupService groupService = new GroupService();
    public AndroidDevice getAndroidDevice() {
        return androidDevice;
    }

    public void setAndroidDevice(AndroidDevice androidDevice) {
        this.androidDevice = androidDevice;
    }

    private AndroidDevice androidDevice;

    public DeviceControlDecorator(AndroidDevice androidDevice) {
        super();
        setAndroidDevice(androidDevice);
        initJSPanel();
    }

    public DeviceControlDecorator(){
        super();
    }

    private void initJSPanel() {
        final JPanelScreen panelScreen = new JPanelScreen(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getControll();
            }
        },new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                removeControll();
            }
        },new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pressHome();
            }
        },new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pressBack();
            }
        });
        jPanelScreen = panelScreen;
        androidDevice.getScreenCaptureThread().setListener(jPanelScreen);
        panelScreen.setVisible(true);
        this.setViewportView(panelScreen);
        this.setPreferredSize(new Dimension(jpsWidth, jpsHight));
        this.setVisible(true);
        androidDevice.getScreenCaptureThread().start();
        refreshData();
    }

    public void getControll() {
        oa = new OperateAndroid(androidDevice.getDevice());
        OperateAndroid.injectListener(jPanelScreen, oa);
    }

    public void removeControll(){
        OperateAndroid.removeListener(jPanelScreen,oa);
        oa=null;
    }

    public void refreshData(){
        DeviceBean db = deviceService.findBySerialNo(androidDevice.getSerialNo());
        jPanelScreen.refreshData( db.getSerialNo(),db.getDname(),groupService.getGroupName(db.getGcode()));
    }

    public void pressHome(){
        oa.press(OperateAndroid.HOME);
    }

    public void pressBack(){
        oa.press(OperateAndroid.BACK);
    }

    public void setjPanelScreen(JPanelScreen jPanelScreen) {
        this.jPanelScreen = jPanelScreen;
    }
}
